import xlrd
import xlwt
from xlutils.copy import copy

FILE_PATH = 'C:\\Users\\Administrator\\Desktop\\wanqh-reptitle\\src\\test\\resources\结果.xls'


def init_result_excel(path, sheet_name_header):
    # workbook = xlrd.open_workbook(path)  #
    workbook = xlwt.Workbook()  # 新建一个工作簿
    for key, value in sheet_name_header.items():
        print(key, value)
        sheet = workbook.add_sheet(key)  # 在工作簿中新建一个表格
        for i in range(0, len(value)):
            sheet.write(0, i, value[i])
    workbook.save(path)
    print("初始化数据成功！")

def write_excel_xls(path, sheet_name, value):
    index = len(value)  # 获取需要写入数据的行数
    # workbook = xlrd.open_workbook(path)  # 打开工作簿
    workbook = xlwt.Workbook()  # 新建一个工作簿
    sheet = workbook.add_sheet(sheet_name)  # 在工作簿中新建一个表格
    for i in range(0, index):
        for j in range(0, len(value[i])):
            sheet.write(i, j, value[i][j])  # 像表格中写入数据（对应的行和列）
    workbook.save(path)  # 保存工作簿
    print("xls格式表格写入数据成功！")

def write_excel_xls_append(path,sheet_name, value):
    index = len(value)  # 获取需要写入数据的行数
    workbook = xlrd.open_workbook(path)  # 打开工作簿
    sheets = workbook.sheet_names()  # 获取工作簿中的所有表格
    print(value)
    worksheet = workbook.sheet_by_name(sheet_name)  # 获取工作簿中所有表格中的的第一个表格
    rows_old = worksheet.nrows  # 获取表格中已存在的数据的行数
    new_workbook = copy(workbook)  # 将xlrd对象拷贝转化为xlwt对象
    new_worksheet = new_workbook.get_sheet(sheet_name)  # 获取转化后工作簿中的第一个表格
    for i in range(0, index):
        for j in range(0, len(value[i])):
            new_worksheet.write(i+rows_old, j, value[i][j])  # 追加写入数据，注意是从i+rows_old行开始写入
    new_workbook.save(path)  # 保存工作簿
    print('xls格式表格【追加】写入数据成功!')


def read_excel_xls(path):
    workbook = xlrd.open_workbook(path)  # 打开工作簿
    sheets = workbook.sheet_names()  # 获取工作簿中的所有表格
    worksheet = workbook.sheet_by_name(sheets[0])  # 获取工作簿中所有表格中的的第一个表格
    for i in range(0, worksheet.nrows):
        for j in range(0, worksheet.ncols):
            print(worksheet.cell_value(i, j), "\t", end="")  # 逐行逐列读取数据
        print()

def isContains(result, keyword):
    if result == 'null':
        return False
    split = keyword.split('/')
    for i in range(0, len(split)):
        if result.lower().find(split[i].lower()) > -1:
            return True
    return False
